{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sample\n",
    "\n",
    "A common problem is that the input format of different models is highly different, making it very difficult to load and utilize data. `Sample` solve this problem by decomposing various NLP task data into underlying `Fields`, which cover all basic input types."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Take SASample as an example\n",
    "from textflint.input_layer.component.sample.sa_sample import SASample\n",
    "\n",
    "data = {'x': 'Titanic is my favorite movie. The leading actor is good.','y': 'pos'}\n",
    "sample = SASample(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`Sample` provides common linguistic functions, including tokenization, partof-speech tagging and dependency parsing,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Titanic', 0, 1, 'PRODUCT')]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample.get_ner('x')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['NNP', 'VBZ', 'PRP$', 'JJ', 'NN', '.', 'DT', 'VBG', 'NN', 'VBZ', 'JJ', '.']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample.get_pos('x')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And `textflint` break down the arbitrary text transformation method into some atomic operations inside `Sample`, backed with clean and consistent implementations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Titanic is my favorite movie. The leading actor is good.'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample.get_text('x')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Titanic',\n",
       " 'is',\n",
       " 'my',\n",
       " 'favorite',\n",
       " 'movie',\n",
       " '.',\n",
       " 'The',\n",
       " 'leading',\n",
       " 'actor',\n",
       " 'is',\n",
       " 'good',\n",
       " '.']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample.get_words('x')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Titanic is my favorite movie.', 'The leading actor is good.']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sample.get_sentences('x')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dataset\n",
    "\n",
    "`Dataset` contains samples and provides efficient and handy operation interfaces for samples. `Dataset` supports loading, verification, and saving data in JSON or CSV format for various NLP tasks. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[34;1mtextflint\u001b[0m: ******Start load!******\n",
      "100%|██████████| 2/2 [00:00<00:00, 976.56it/s]\n",
      "\u001b[34;1mtextflint\u001b[0m: 2 in total, 2 were loaded successful.\n",
      "\u001b[34;1mtextflint\u001b[0m: ******Finish load!******\n"
     ]
    }
   ],
   "source": [
    "from textflint.input_layer.dataset import Dataset\n",
    "data2 = {'x': 'I don\\'t like the actor Tim Hill', 'y': 'neg'}\n",
    "dataset = Dataset('SA')\n",
    "dataset.load([data, data2])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
